package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DataCourseParamHistory;
import com.uwlaser.service.DataCourseParamHistoryService;
import com.uwlaser.service.dto.DataCourseParamHistoryDto;
import com.uwlaser.service.dto.DataCourseParamHistoryQueryParam;
import com.uwlaser.service.mapper.DataCourseParamHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DataCourseParamHistoryService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DataCourseParamHistoryServiceImpl extends CommonServiceImpl<DataCourseParamHistoryMapper, DataCourseParamHistory> implements DataCourseParamHistoryService {

    // private final RedisUtils redisUtils;
    private final DataCourseParamHistoryMapper dataCourseParamHistoryMapper;

    @Override
    public PageInfo<DataCourseParamHistoryDto> queryAll(DataCourseParamHistoryQueryParam query, Pageable pageable) {
        IPage<DataCourseParamHistory> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DataCourseParamHistory> page = dataCourseParamHistoryMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DataCourseParamHistoryDto.class);
    }

    @Override
    public List<DataCourseParamHistoryDto> queryAll(DataCourseParamHistoryQueryParam query){
        return ConvertUtil.convertList(dataCourseParamHistoryMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DataCourseParamHistoryDto.class);
    }

    @Override
    public DataCourseParamHistory getById(Integer id) {
        return dataCourseParamHistoryMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DataCourseParamHistoryDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DataCourseParamHistoryDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DataCourseParamHistoryDto resources) {
        DataCourseParamHistory entity = ConvertUtil.convert(resources, DataCourseParamHistory.class);
        return dataCourseParamHistoryMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DataCourseParamHistoryDto resources){
        DataCourseParamHistory entity = ConvertUtil.convert(resources, DataCourseParamHistory.class);
        int ret = dataCourseParamHistoryMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return dataCourseParamHistoryMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DataCourseParamHistoryDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DataCourseParamHistoryDto dataCourseParamHistory : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品SN", dataCourseParamHistory.getProductSn());
              map.put("产品型号", dataCourseParamHistory.getProductCode());
              map.put("产品类型", dataCourseParamHistory.getProductType());
              map.put("订单编号", dataCourseParamHistory.getOrderCode());
              map.put("工单编号", dataCourseParamHistory.getWorkOrderCode());
              map.put("产线编号", dataCourseParamHistory.getLienCode());
              map.put("工段编号", dataCourseParamHistory.getSectionCode());
              map.put("工位编号", dataCourseParamHistory.getStationCode());
              map.put("工序编号", dataCourseParamHistory.getOperationCode());
              map.put("设备编号", dataCourseParamHistory.getDeviceCode());
              map.put("判断结果", dataCourseParamHistory.getParamResult());
              map.put("参数编码", dataCourseParamHistory.getParamCode());
              map.put("参数名称", dataCourseParamHistory.getParamName());
              map.put("参数值", dataCourseParamHistory.getParamValue());
              map.put("判断范围", dataCourseParamHistory.getParamRange());
              map.put("参数单位", dataCourseParamHistory.getParamUnit());
              map.put("测试顺序", dataCourseParamHistory.getParamNumber());
              map.put("过站ID", dataCourseParamHistory.getPassStationId());
              map.put("创建人", dataCourseParamHistory.getCreateBy());
              map.put("创建时间", dataCourseParamHistory.getCreateTime());
              map.put("更新人", dataCourseParamHistory.getUpdateBy());
              map.put("更新时间", dataCourseParamHistory.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
